import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Router Modules */
/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [

  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/projectLogin',
    component: () => import('@/views/login/projectLogin'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/home/index'),
        name: 'home',
        meta: {
          title: '首页',
          icon: 'el-icon-price-tag',
          roles: ['admin'], // you can set roles in root nav
          affix: true
        }
      }
    ]
  },
  // 用户管理
  {
    path: '/user',
    component: Layout,
    redirect: '/user/customerService',
    alwaysShow: true, // will always show the root menu
    name: '用户管理',
    meta: {
      title: '用户管理',
      icon: 'el-icon-user',
      roles: ['admin'] // you can set roles in root nav
    },
    children: [
      {
        path: 'customerService',
        component: () => import('@/views/user/customerService'),
        name: 'customerService',
        meta: { title: '客服信息',  affix: true}
      },
      {
        path: 'userInfo',
        component: () => import('@/views/user/userInfo'),
        name: 'userInfo',
        meta: { title: '用户信息',  affix: true}
      }
    ]
  },
  // 账号管理
  {
    path: '/account',
    component: Layout,
    redirect: '/account/card',
    alwaysShow: true, // will always show the root menu
    name: '卡号管理',
    meta: {
      title: '卡号管理',
      icon: 'el-icon-postcard',
      roles: ['admin'], // you can set roles in root nav
    },
    children: [
      {
        path: 'card',
        component: () => import('@/views/account/card'),
        name: 'card',
        meta: { title: '卡号信息',  affix: true }
      },
      {
        path: 'expireCard',
        component: () => import('@/views/account/expireCard'),
        name: 'expireCard',
        meta: { title: '到期卡号', affix: true }
      }
    ]
  },
  // 设备管理
  {
    path: '/device',
    component: Layout,
    redirect: '/device/stockDevice',
    alwaysShow: true, // will always show the root menu
    name: '设备管理',
    meta: {
      title: '设备管理',
      icon: 'el-icon-set-up',
      roles: ['admin', 'custom' ] // you can set roles in root nav
    },
    children: [
      {
        path: 'stockDevice',
        component: () => import('@/views/device/storeDevice'),
        name: 'stockDevice',
        meta: { title: '库存设备',  affix: true, roles: ['admin'] }
      },
      {
        path: 'outboundDevice',
        component: () => import('@/views/device/outboundDevice'),
        name: 'outboundDevice',
        meta: { title: '出库设备',  affix: true, roles: [ 'admin', 'custom' ] }
      },
      {
        path: 'saleDevice',
        component: () => import('@/views/device/saleDevice'),
        name: 'saleDevice',
        meta: { title: '已销售设备',  affix: true, roles: ['admin', 'custom' ] }
      },
      {
        path: 'installDevice',
        component: () => import('@/views/device/installDevice'),
        name: 'installDevice',
        meta: { title: '已安装设备',  affix: true, roles: ['admin', 'custom' ] }
      },
      {
        path: 'returnDevice',
        component: () => import('@/views/device/returnDevice'),
        name: 'returnDevice',
        meta: { title: '返库设备',  affix: true, roles: ['admin'] }
      }
    ]
  },

  // 设备巡检
  {
    path: '/device',
    component: Layout,
    name: 'device',
    meta: { title: '设备巡检', icon: 'el-icon-news', roles: ['admin']},
    children: [
      {
        path: 'deviceInspection',
        component: () => import('@/views/device/deviceInspection'),
        name: 'deviceInspection',
        meta: { title: '设备巡检', icon: 'el-icon-news', roles: ['admin'], affix: true }
      }
    ]
  },

  // 设备数据
  {
    path: '/deviceInfo',
    component: Layout,
    redirect: '/deviceInfo/deviceData',
    alwaysShow: true, // will always show the root menu
    name: '设备数据',
    meta: {
      title: '设备数据',
      icon: 'el-icon-copy-document',
      roles: ['admin' ] // you can set roles in root nav
    },
    children: [
      {
        path: 'deviceData',
        component: () => import('@/views/deviceInfo/deviceData'),
        name: 'deviceData',
        meta: { title: '实时数据', affix: true}
      },
      {
        path: 'alertData',
        component: () => import('@/views/deviceInfo/alertData'),
        name: 'alertData',
        meta: { title: '告警数据', affix: true}
      }
    ]
  },

  // 项目管理
  {
    path: '/project',
    component: Layout,
    redirect: '/project/projectAdmin',
    alwaysShow: false, // will always show the root menu
    name: 'project',
    meta: {
      title: '项目管理',
      icon: 'el-icon-setting',
      roles: ['admin'] // you can set roles in root nav
    },
    children: [
      {
        path: '/project/projectAdmin',
        component: () => import('@/views/project/projectAdmin'),
        name: 'projectAdmin',
        meta: {
          title: '项目管理',
          icon: 'el-icon-setting',
          roles: ['admin'], // you can set roles in root
        }
      },
      {
        path: '/project/projectAdmin/:id/:name',
        component: () => import('@/views/project/detail'),
        name: 'projectDetail',
        hidden: true,
        meta: {
          title: '项目详情',
          roles: ['admin'], // you can set roles in root
        }
      },

    ]
  },

  // 我的项目
  {
    path: '/myProject',
    component: Layout,
    redirect: '/myProject/graph',
    alwaysShow: false, // will always show the root menu
    name: 'myProject',
    meta: {
      title: '我的项目',
      icon: 'el-icon-setting',
      roles: ['admin', 'custom'] // you can set roles in root nav
    },
    children: [
      {
        path: 'graph',
        component: () => import('@/views/myProject/projectGraph'),
        name: 'projectGraph',
        meta: {
          title: '我的项目',
          icon: 'el-icon-setting',
          roles: ['admin', 'custom'], // you can set roles in root
          // navaffix: true
        },
      },
      {
        path: '/myProject/graph/:id/:name',
        component: () => import('@/views/myProject/detail'),
        name: 'myProjectDetail',
        hidden: true,
        meta: {
          title: '项目详情',
          roles: ['admin', 'custom'], // you can set roles in root
        }
      },
    ]
  },
  {
    path: '/projectMap',
    component: Layout,
    // alwaysShow: false, // will always show the root menu
    name: 'projectMap',
    hidden: true,
    meta: {
      title: '我的项目',
      icon: 'el-icon-setting',
      roles: ['admin', 'custom'] // you can set roles in root nav
    },
    children: [
      {
        path: '/projectMap/:id',
        component: () => import('@/views/myProject/detail'),
        name: 'map',
        meta: {
          title: '项目详情',
          icon: 'el-icon-setting',
          roles: ['admin', 'custom'], // you can set roles in root
          // navaffix: true
        },
      }

    ]
  },

  //系统日志 admin 权限显示
  {
    path: '/system',
    component: Layout,
    name: 'system',
    meta: { title: '系统日志', icon: 'el-icon-time', roles: ['admin']},
    children: [
      {
        path: 'systemLogo',
        component: () => import('@/views/system/systemLogo'),
        name: 'systemLogo',
        meta: { title: '系统日志', icon: 'el-icon-time', roles: ['admin'], affix: true }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  // mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
